/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.rms.recordstores.calendar;

import java.util.Date;
import java.util.Vector;

import mtt.rms.dataObjects.Event;

/**
 * This Class represents a Week
 * 
 * @author jens.meiss
 */
public class Week {

	/**
	 * contains the Number of the Week in the year (1-52)
	 */
	private byte weekNumber;

	/**
	 * contains the Date of the first Day of the Week
	 */
	private Date dateFirstDayOfWeek;

	/**
	 * contains the Date of the last Day of the Week
	 */
	private Date dateLastDayOfWeek;

	/**
	 * 
	 */
	public static byte Monday = 0;

	/**
	 * 
	 */
	public static byte Thuseday = 1;

	/**
	 * 
	 */
	public static byte Wednesday = 2;

	/**
	 * 
	 */
	public static byte Thursday = 3;

	/**
	 * 
	 */
	public static byte Friday = 4;

	/**
	 * 
	 */
	public static byte Saturday = 5;

	/**
	 * 
	 */
	public static byte Sunday = 6;

	/**
	 * containing an Vector of Events for Every Day in the Week, beginning with
	 * Monday
	 */
	private Vector[] vectors = { new Vector(), new Vector(), new Vector(),
			new Vector(), new Vector(), new Vector(), new Vector() };

	/**
	 * 
	 */
	public static final int millisOfASecond = 1000;

	/**
	 * 
	 */
	public static final int millisOfAMinute = 60 * millisOfASecond; /*
																	 * 60 * 1000 =
																	 * 60000
																	 */

	/**
	 * 
	 */
	public static final int millisOfAHour = 60 * millisOfAMinute; /*
																	 * 60 *
																	 * 60000 =
																	 * 3600000
																	 */

	/**
	 * contains the count of Milliseconds of a Day
	 */
	public static final long millisOfADay = 24 * millisOfAHour; /*
																 * 1000 * 60 *
																 * 60 * 24 =
																 * 86400000
																 */

	public static final long millisOfAWeek = 7 * millisOfADay;

	public byte getWeekNumber() {
		return weekNumber;
	}

	public void setWeekNumber(byte weekNumber) {
		this.weekNumber = weekNumber;
	}

	public Date getDateFirstDayOfWeek() {
		return dateFirstDayOfWeek;
	}

	public void setDateFirstDayOfWeek(Date dateFirstDayOfWeek) {
		this.dateFirstDayOfWeek = dateFirstDayOfWeek;
	}

	public Date getDateLastDayOfWeek() {
		return dateLastDayOfWeek;
	}

	public void setDateLastDayOfWeek(Date dateLastDayOfWeek) {
		this.dateLastDayOfWeek = dateLastDayOfWeek;
	}

	/**
	 * 
	 * @param event
	 * @return added the event?
	 */
	public boolean addEvent(Event event) {

		/*
		 * ist das event für die aktuelle woche gültig
		 */
		/*
		 * liegt das event in der woche?
		 */
		if (	(event.getDateStartOfEvent().getTime() >= getDateLastDayOfWeek().getTime())
			&& 	(event.getDateEndOfEvent().getTime()   <= getDateFirstDayOfWeek().getTime())) {
			return false;
		}
		/*
		 * für jedes im Intervall liegende Event die Time berechnen und prüfen
		 * ob sie zwichen dateFirsDayOfWeek und dateLastDayOfWeek
		 */
		long intervallMillis = event.getDateStartOfEvent().getTime() + event.getTimeBeginOfEvent();
		while (intervallMillis <= getDateLastDayOfWeek().getTime()) {

			intervallMillis += event.getIntervall() * millisOfAWeek;

			if ((intervallMillis >= getDateFirstDayOfWeek().getTime())
					&& (intervallMillis <= getDateLastDayOfWeek().getTime())) {

				/*
				 * Tag an dem das Event stattfindet bestimmen
				 */
				long endTimeDay = getDateFirstDayOfWeek().getTime()  + millisOfADay - 1;
				byte count = 0;
				while (count < 7) {

					if (intervallMillis < endTimeDay) {
						addEventToDay(count, event);
						return true;
					}

					endTimeDay += millisOfADay;
					count++;
				}
			}
		}
		
		return false;
	}

	private void addEventToDay(byte day, Event event) {

		vectors[day].addElement(event);

	}
	
	public Vector getVectorEvents(byte day) {
		return vectors[day];
	}
}
